package cn.edu.nju.software.paymentSystem;

import java.util.MissingResourceException;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;

import cn.edu.nju.software.sellerSystem.SystemB;
import cn.edu.nju.software.sellerSystem.SystemBHelper;

public class SellerClient {

	ORB orb;
	POA rootPOA;
	NamingContextExt ncRef;
	SystemB sysb;

	/**
	 * @param name
	 *            the name of CORBA object
	 * @return -return the CORBA Object
	 */
	public SystemB init(String name) {
		String initArgs[] = new String[4];
		try {
			initArgs[0] = PaymentOptions.SELLER_CORBA_INIT_ARG_0;
			initArgs[1] = PaymentOptions.SELLER_CORBA_INIT_ARG_1;
			initArgs[2] = PaymentOptions.SELLER_CORBA_INIT_ARG_2;
			initArgs[3] = PaymentOptions.SELLER_CORBA_INIT_ARG_3;
			// create an ORB instance,telling it the Name Service's location
			this.orb = ORB.init(initArgs, null);

			// get the root naming context
			org.omg.CORBA.Object objRef = orb
					.resolve_initial_references("NameService");
			// Use NamingContextExt instead of NamingContext. This is
			// part of the Interoperable naming Service.
			ncRef = NamingContextExtHelper.narrow(objRef);

			// resolve the Object Reference in Naming
			this.sysb = SystemBHelper.narrow(ncRef.resolve_str(name));
			//System.out.println("Obtained a handle of Seller Service OBJ.");
			return this.sysb;
		} catch (MissingResourceException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
